package com.example.algorithm_learning_platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.algorithm_learning_platform.common.Result;
import com.example.algorithm_learning_platform.entity.AlgorithmCase;
import com.example.algorithm_learning_platform.service.AlgorithmCaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "算法案例管理", description = "管理算法案例，包括伪代码、实现和复杂度分析")
@RestController
@RequestMapping("/api/algorithm-cases")
@RequiredArgsConstructor
public class AlgorithmCaseController {

    private final AlgorithmCaseService algorithmCaseService;

    @Operation(summary = "创建算法案例")
    @PostMapping
    public Result<AlgorithmCase> create(@RequestBody AlgorithmCase algorithmCase) {
        algorithmCaseService.save(algorithmCase);
        return Result.success(algorithmCase);
    }

    @Operation(summary = "获取指定算法类型的所有案例")
    @GetMapping("/by-type/{typeId}")
    public Result<List<AlgorithmCase>> listByType(
            @Parameter(description = "算法类型ID") @PathVariable Integer typeId) {
        LambdaQueryWrapper<AlgorithmCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlgorithmCase::getTypeId, typeId);
        return Result.success(algorithmCaseService.list(queryWrapper));
    }

    @Operation(summary = "获取案例详情")
    @GetMapping("/{id}")
    public Result<AlgorithmCase> getById(@Parameter(description = "案例ID") @PathVariable Integer id) {
        return Result.success(algorithmCaseService.getById(id));
    }

    @Operation(summary = "更新算法案例")
    @PutMapping("/{id}")
    public Result<AlgorithmCase> update(
            @Parameter(description = "案例ID") @PathVariable Integer id,
            @RequestBody AlgorithmCase algorithmCase) {
        algorithmCase.setCaseId(id);
        algorithmCaseService.updateById(algorithmCase);
        return Result.success(algorithmCase);
    }

    @Operation(summary = "删除算法案例")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "案例ID") @PathVariable Integer id) {
        return Result.success(algorithmCaseService.removeById(id));
    }

    @Operation(summary = "分页查询算法案例")
    @GetMapping("/page")
    public Result<Page<AlgorithmCase>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "算法类型ID") @RequestParam(required = false) Integer typeId,
            @Parameter(description = "案例名称") @RequestParam(required = false) String caseName) {
        
        LambdaQueryWrapper<AlgorithmCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(typeId != null, AlgorithmCase::getTypeId, typeId)
                   .like(caseName != null, AlgorithmCase::getCaseName, caseName);
        return Result.success(algorithmCaseService.page(new Page<>(page, size), queryWrapper));
    }
} 